'use strict';
const Controller = require('egg').Controller;
// const { generateRandomNumber } = require('../utils/randomUUID');
class ReservationController extends Controller {
  // 查询所有的订单信息
  async index() {
    const { ctx, service, app } = this;
    const { page, pageSize } = this.ctx.query;
    const { count } = await this.ctx.model.Reservation.findAndCountAll();
    const totalPrice = await app.model.Reservation.sum('total_price');
    if (page && pageSize) {
      const reservationInfo = await service.reservation.getReservationList(page, pageSize);
      ctx.body = {
        code: 200,
        msg: 'ok',
        data: reservationInfo,
        pageInfo: {
          page, // 查询起始位置
          pageSize,
        }, // 查询的返回条数}
        total: count,
        totalPrice,
      };
    } else {
      const reservationInfo2 = await service.reservation.getReservationList();
      ctx.body = {
        code: 200,
        msg: 'ok',
        data: reservationInfo2,
        total: count,
        totalPrice,
      };
    }

    // console.log(reservationInfo);


  }
  // 根据订单编号查询订单信息
  async show() {
    const { ctx, service } = this;
    const orderID = ctx.params.id;

    const reserInfoById = await service.reservation.getReserListById(orderID);
    ctx.body = {
      code: 200,
      msg: 'ok',
      data: reserInfoById,
    };
  }
  // 创建订单
  async create() {
    try {
      const { ctx, service } = this;
      // const data = this.ctx.request.body;
      const name = ctx.request.body.name;
      const phone = ctx.request.body.phone;
      const room_title = ctx.request.body.title;
      const room_id = ctx.request.body.roomid;

      const total_price = ctx.request.body.total_price;
      // const orderState = ctx.request.body.orderState;
      const checkin_date = ctx.request.body.date1;
      const checkout_date = ctx.request.body.date2;
      const hotel_title = ctx.request.body.hotel_name;
      const hotel_address = ctx.request.body.hotel_address;
      const user_id = ctx.request.body.user_id;
      const room_type = ctx.request.body.room_type;
      const textarea = ctx.request.body.textarea;
      const hotel_id = ctx.request.body.hotel_id;
      const room_pic = ctx.request.body.room_pic;
      // 前端传过来的数据 用于生成订单
      const list = await service.reservation.addReserList({ name, phone, room_title, room_id, total_price, checkin_date, checkout_date, hotel_title, hotel_address, user_id, room_type, textarea, hotel_id, room_pic });
      // console.log(list);
      if (list) {
        this.ctx.body = {
          code: 200,
          msg: '添加订单成功',
          data: list,
        };
      } else {
        this.ctx.body = {
          code: 403,
          msg: '添加订单失败',
        };
      }
    } catch (error) {
      return error;
    }
  }
  // 根据订单编号 删除订单
  async destroy() {
    try {
      const { ctx, service } = this;
      const orderID = ctx.params.id;

      const delResult = await service.reservation.delReservation(orderID);
      if (delResult) {
        ctx.body = {
          code: 200,
          msg: '删除订单成功',
        };
      } else {
        ctx.body = {
          code: 400,
          msg: '删除订单失败',
        };
      }
    } catch (error) {
      this.ctx.body = {
        code: 500,
        msg: '服务器发生错误，请联系管理人员',
      };
    }
  }
  // 根据订单编号修改订单
  async update() {
    const { ctx, service } = this;
    try {
      const orderID = ctx.params.id;
      const data = ctx.request.body;

      const updateResult = await service.reservation.updateReservation(orderID, data);
      if (updateResult) {
        ctx.body = {
          code: 200,
          msg: '订单信息修改成功',
          data: updateResult,
        };
      } else {
        ctx.body = {
          code: 400,
          msg: '订单信息修改失败',

        };
      }
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: '服务器的控制器代码错误，请检查',
        error,
      };
    }
  }
  // 房间id查询订单
  async getReverInfoByRoomId() {
    const { ctx } = this;
    const id = ctx.params.id;
    const result = await this.app.model.Reservation.findOne({ where: { room_id: id } });
    if (result) {
      ctx.body = {
        code: 200,
        data: result,
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '该订单不存在',
      };
    }

  }
  // 用户id查询订单
  async getReverInfoByUserId() {
    const { ctx } = this;
    const id = ctx.params.id;
    const result = await this.app.model.Reservation.findAll({ where: { user_id: id } });
    if (result) {
      ctx.body = {
        code: 200,
        data: result,
      };
    } else {
      ctx.body = {
        code: 400,
        msg: '该订单不存在',
      };
    }

  }
}
module.exports = ReservationController;
